// 计划入库商品
var tblStockInSkuPlanInfo;
// 实际入库商品
var tblStockInSkuRealInfo;

// 页面初始化
$(function () {
    
    // 默认基本信息显示
	showStockInSkuPlanInfoTab();
    
    // tab切换事件
    $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
        
        switch ($(this).attr("id")) {
        case "stockInInfo":    //基本信息
            showStockInInfoTab();
            break;
        case "stockInSkuPlanInfo":    //计划入库商品
        	showStockInSkuPlanInfoTab();
            break;
        case "stockInSkuRealInfo":    //实际入库商品
            showStockInSkuRealInfoTab();
            break;
        case "skuBatchInfo":    //实际入库商品
            showSkuBatchInfo();
            break;
        case "noteInfo":    //内部标签
            showNoteInfo();
            break;             
        default:
            break;
        }
    });
    
});

// 单价，金额格式化
var numberParser = function(data){
    var returnValue = "-";
    var numberData = parseFloat(data);
    if(numberData===+numberData){
        returnValue = numberData.toFixed(2);
    }
    return returnValue;
};

// 基本信息
function showStockInInfoTab() {
    var stockInId = $('#stockin_id', window.parent.document).val();

    $.ajax({
        type: "GET",
        url: "/cainiaoStockIn/getStockInInfo/" + stockInId + ".action",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,//同步
        success: function (data) {
            if (data.result == "success") {
                $("#stockinCode").text(data.data.stockinCode);
                $("#wmsStockinCode").text(data.data.wmsStockinCode); 
                if (data.data.stockinType == "pu") {
                    $("#stockinType").text("采购入库");
                } else {
                	$("#stockinType").text("调拨入库");
                }
                $("#suppName").text(data.data.suppName); 
                if (data.data.purcType == "dx") {
                    $("#purcType").text("代销采购");
                } else if (data.data.purcType == "xy") {
                    $("#purcType").text("信用采购");
                } else {
                	$("#purcType").text("预付采购");
                }
                $("#storName").text(data.data.storName);
                if (data.data.createTime != null) {
                    $("#createTime").text(data.data.createTime);
                }
                if (data.data.apprTime != null) {
                    $("#apprTime").text(data.data.apprTime);
                }
                if (data.data.factInTime != null) {
                    $("#factInTime").text(data.data.factInTime);
                }
                $("#lgstName").text(data.data.lgstName); 
                $("#lgstNo").text(data.data.lgstNo); 
                $("#deliveryman").text(data.data.deliveryman); 
                $("#deliverymanMobile").text(data.data.deliverymanMobile); 
                $("#deliverymanTel").text(data.data.deliverymanTel); 
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
}

// 计划入库商品
function showStockInSkuPlanInfoTab(){
    if (tblStockInSkuPlanInfo!=null && tblStockInSkuPlanInfo != undefined && ""!=tblStockInSkuPlanInfo){
    	tblStockInSkuPlanInfo.draw();
        return;
    }
    
    var stockInId = $('#stockin_id', window.parent.document).val();
    
    tblStockInSkuPlanInfo = $('#tblStockInSkuPlanInfo').DataTable({
        "bPaginate": false,
        "processing": true,
        "serverSide": true,
        "dom": '<"bottom"rtfli>',
        "searching": false,
        "deferRender": true,
        "info": false,
        "ajax": {
            "url": "/cainiaoStockIn/getStockInGoodsList.action",
            "data": function ( d ) {
                d.stockinId = encodeURI(stockInId);
                d.kind = encodeURI("plan");
            }
        },
        "tableTools": {
            "sRowSelect": "bootstrap"
        },
        "columns": [
            {"data": "goodsCode"},
            {"data": "goodsName"},
            {"data": "skuCode"},
            {"data": "skuName"},
            {"data": "qty"},
            {"data": "inventoryType", 
                "render": function ( data, type, row, meta ) {
                    if (row.inventoryType == "zp") {
                        return "正品";
                    } else {
                        return "次品";
                    }
                }
            },
            {"data": "stanPurcPrice", "render": numberParser},
            {"data": "purcPrice", "render": numberParser},
            {"data": "purcAmount", "render": numberParser},
        ],
        "columnDefs": [
            {"className": "truncate-text tooltip-column-detail-5", "targets":[0]},
            {"className": "truncate-text tooltip-column-detail", "targets":[1]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[2]},
            {"className": "truncate-text tooltip-column-detail", "targets":[3]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[4]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[5]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[6]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[7]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[8]}
        ],
        "order": [[0, "asc"]]
    });
    
    // 文字过长截断显示悬浮框
    $("#tblStockInSkuPlanInfo").tooltip({
        selector: ".tooltip-column-detail-5,.tooltip-column-detail",
        container: "body",
        title: function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });
}

// 实际入库商品
function showStockInSkuRealInfoTab(){
    if (tblStockInSkuRealInfo!=null && tblStockInSkuRealInfo != undefined && ""!=tblStockInSkuRealInfo){
    	tblStockInSkuRealInfo.draw();
        return;
    }
    
    var stockInId = $('#stockin_id', window.parent.document).val();
    
    tblStockInSkuRealInfo = $('#tblStockInSkuRealInfo').DataTable({
        "bPaginate": false,
        "processing": true,
        "serverSide": true,
        "dom": '<"bottom"rtfli>',
        "searching": false,
        "deferRender": true,
        "info": false,
        "ajax": {
            "url": "/cainiaoStockIn/getStockInGoodsList.action",
            "data": function ( d ) {
                d.stockinId = encodeURI(stockInId);
                d.kind = encodeURI("real");
            }
        },
        "tableTools": {
            "sRowSelect": "bootstrap"
        },
        "columns": [
            {"data": "goodsCode"},
            {"data": "goodsName"},
            {"data": "skuCode"},
            {"data": "skuName"},
            {"data": "qty"},
            {"data": "inventoryType", 
                "render": function ( data, type, row, meta ) {
                    if (row.inventoryType == "zp") {
                        return "正品";
                    } else {
                        return "次品";
                    }
                }
            },
            {"data": "stanPurcPrice", "render": numberParser},
            {"data": "purcPrice", "render": numberParser},
            {"data": "purcAmount", "render": numberParser},
        ],
        "columnDefs": [
            {"className": "truncate-text tooltip-column-detail-5", "targets":[0]},
            {"className": "truncate-text tooltip-column-detail", "targets":[1]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[2]},
            {"className": "truncate-text tooltip-column-detail", "targets":[3]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[4]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[5]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[6]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[7]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[8]},
        ],
        "order": [[0, "asc"]]
    });
    
    // 文字过长截断显示悬浮框
    $("#tblStockInSkuRealInfo").tooltip({
        selector: ".tooltip-column-detail-5,.tooltip-column-detail",
        container: "body",
        title: function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });
}

// 内部标签
function showNoteInfo() {
    var stockInId = $('#stockin_id', window.parent.document).val();
    var ifmSrc = "/views/base/note/notelist.html?busiKey=" + stockInId;
    $("#noteIframe").attr("src", ifmSrc);
}

// 批次效期
function showSkuBatchInfo() {
    var stockInId = $('#stockin_id', window.parent.document).val();
    var ifmSrc = "/views/base/batch/batchlist.html?busiKey=" + stockInId;
    $("#skuBatchIframe").attr("src", ifmSrc);
}
